// DialogOpenURL.cpp : implementation file
//

#include "stdafx.h"
#include "MMSRecorder.h"
#include "MainFrm.h"
#include "DialogOpenURL.h"


// CDialogOpenURL dialog

IMPLEMENT_DYNAMIC(CDialogOpenURL, CDialog)

CDialogOpenURL::CDialogOpenURL(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogOpenURL::IDD, pParent)
{
}

CDialogOpenURL::~CDialogOpenURL()
{
}

void CDialogOpenURL::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CDialogOpenURL, CDialog)
	ON_BN_CLICKED(IDC_BUTTON1, &CDialogOpenURL::OnBrowse)
	ON_EN_UPDATE(IDC_EDIT_SAVEPATH, &CDialogOpenURL::OnEnUpdateEdit)
	ON_EN_UPDATE(IDC_EDIT_URL, &CDialogOpenURL::OnEnUpdateEdit)
//	ON_WM_CREATE()
	ON_WM_ACTIVATE()
END_MESSAGE_MAP()


// CDialogOpenURL message handlers

void CDialogOpenURL::OnBrowse()
{
	// TODO: Add your control notification handler code here
	/*
	SetDlgItemText(IDC_EDIT_URL, L"mms://media.chinabroadcast.cn/en/webextra/2009/03/0320huaxu.wmv");
	// SetDlgItemText(IDC_EDIT_URL, L"mms://mencode.xinhuanet.com/live2?cccode=cc1280");
	//SetDlgItemText(IDC_EDIT_URL, L"f:\\a.wmv");
	SetDlgItemText(IDC_EDIT_SAVEPATH, L"f:\\1.asf");
	*/
	wchar_t szFile[MAX_PATH] = L"mms.asf";
	OPENFILENAME Ofn;
	memset(&Ofn, 0, sizeof(Ofn));
	Ofn.lStructSize = sizeof(Ofn); 
	Ofn.hwndOwner = m_hWnd; 
	Ofn.lpstrFile= szFile; 
	Ofn.nMaxFile = _countof(szFile);
	Ofn.lpstrInitialDir = NULL; 
	Ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT; 
	Ofn.lpstrTitle = NULL; 

	if (GetSaveFileName(&Ofn)){
		SetDlgItemText(IDC_EDIT_SAVEPATH, szFile);
	}else{
		DWORD err = CommDlgExtendedError();
	}
}

void CDialogOpenURL::OnOK(void)
{
	CString url, path;
	GetDlgItemText(IDC_EDIT_URL, url);
	GetDlgItemText(IDC_EDIT_SAVEPATH, path);
	
	CMainFrame *mainFrm = (CMainFrame *)theApp.GetMainWnd();
	mainFrm->StartRecording(url, path);

	CDialog::OnOK();
}

void CDialogOpenURL::OnEnUpdateEdit()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.

	// TODO:  Add your control notification handler code here
	wchar_t path[MAX_PATH], url[MAX_PATH];
	if (GetDlgItemText(IDC_EDIT_SAVEPATH, path, _countof(path)) > 0 && path[0] != L'\0'
			&& GetDlgItemText(IDC_EDIT_URL, url, _countof(path)) > 0 && url[0] != L'\0'){
				::EnableWindow(GetDlgItem(IDOK)->m_hWnd, TRUE);
	}else{
		::EnableWindow(GetDlgItem(IDOK)->m_hWnd, FALSE);
	}
}

//int CDialogOpenURL::OnCreate(LPCREATESTRUCT lpCreateStruct)
//{
//	if (CDialog::OnCreate(lpCreateStruct) == -1)
//		return -1;
//
//	// TODO:  Add your specialized creation code here
//	return 0;
//}

void CDialogOpenURL::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
	CDialog::OnActivate(nState, pWndOther, bMinimized);

	// TODO: Add your message handler code here
	::EnableWindow(GetDlgItem(IDOK)->m_hWnd, FALSE);

	SetDlgItemText(IDC_EDIT_URL, L"mms://mencode.xinhuanet.com/live2?cccode=cc1280");
}
